const app = getApp()
var event = require('../../../utils/event.js')
import Config from '../../../utils/config2.js'
import Serv from './pwdCtrlServ';
var regeneratorRuntime = require("../../../lib/js/runtime-module.js")
// pages/biz/pwdCtrl/pwdCtrl.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fromPage: '',
    //触发事件名称
    lisenedEvntName: '',
    //密码控件地址
    pwdCtrlH5Url: Config.pwdCtrlH5Url,
    host: '',
    pwdRandom: ''
  },

  receviceMsg(e){
    let options=this.data.options
    let detail = e.detail.data[0];
    //传递密码密文
    event.emit(this.data.lisenedEvntName, {
      pwdMw: detail.pwdMw||'',
      randomKey: detail.randomKey||'',
      pwdlen: detail.pwdlen||0,
      pwdHash: detail.pwdHash||'',
      ...options
    });
    console.log('receviceMsg:', e.detai);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options = {}) {

    let timestamp = Date.parse(new Date());
    let randomExp = wx.getStorageSync('pwdRandomExp')
    var random = wx.getStorageSync('pwdRandom')
    console.log(randomExp < timestamp, random, randomExp, timestamp )
    if (!random || randomExp < timestamp) {

      let {data = {}, success} = await Serv.getRandom({
        user_id: wx.getStorageSync('userId'),
				flag_chnl: 'H5',
      });
      if (success) {
        let curTimestamp = Date.parse(new Date());
        let expTimestamp = curTimestamp + 1000 * 60 * Config.pwdRandomTimeout;
        random = encodeURI(JSON.stringify({
          license: data.license,
          rsaPublicKey: data.rsa_public_content,
          passGuardId: data.map_arr,
          randomValue: data.random_value,
          randomKey: data.random_key
        }))
        wx.setStorageSync('pwdRandomExp', expTimestamp)
        wx.setStorageSync('pwdRandom', random)
      }
    }
    this.setData({
      fromPage: options.from,
      lisenedEvntName: options.eventName,
      options:options||{},
      host: Config.host,
      pwdRandom: random
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
